package com.example.tool.util.sf;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.HttpClientUtil;
import com.sf.csim.express.service.code.HZTServiceCodeEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 顺丰
 */
public class SFApiUtil {
    public static String CLIENT_CODE = "YBSPMZAEJ5YD";  //此处替换为您在丰桥平台获取的顾客编码
//    public static String CHECK_WORD = "d4ZdBoOtfxju1LDEtPwwHBvQori84yed";//生产校验码
//
//    public static String CALL_URL = "https://sfapi.sf-express.com/std/service"; //生产环境的地址 -PRO


    public static String CALL_URL = "https://sfapi-sbox.sf-express.com/std/service";//沙箱环境的地址 -PRO
    public static String CHECK_WORD = "jX6WpQ8mtTpCYB5TMCVNLw7btK29YlfP";//沙箱校验码
    /**
     track_first = {
     '中通': ['78', '73', '76'],
     '顺丰': 'SF',
     '圆通': 'YT',
     '申通': '77',
     '韵达': ['46', '43', '32', '31'],
     'EMS': ['12', '98', '13'],
     '京东': 'JD',
     '极兔': 'JT',
     '菜鸟': '62',
     }
     * @param args
     * @throws UnsupportedEncodingException
     */
  /*  public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(judgeMailType("Sf45454545"));
    }*/
    public static String judgeMailType(String mailNo){
        String mailNoType="";
        String mailNoPrefix="";
        if(StringUtils.isNotBlank(mailNo)&&mailNo.length()>3){mailNoPrefix=mailNo.substring(0,2).toUpperCase();}
        switch(mailNoPrefix){
        case"78":case"73":case"76":mailNoType="ZT";break;//中通
        case"SF":mailNoType="SF";break;//顺丰
        case"YT":mailNoType="YT";break;//圆通
        case"77":mailNoType="ST";break;//申通
        case"46":case"43":case"32":case"31":mailNoType="YD";break;//韵达
        case"12":case"98":case"13":mailNoType="EMS";break;//EMS
        case"JD":mailNoType="JD";break;//京东
        case"JT":mailNoType="JT";break;//极兔
        case"62":mailNoType="CN";break;//菜鸟
        default:break;}
        return mailNoType;
    }
    /**
     *
     * 查询顺丰路由信息
     * @return
     * @throws UnsupportedEncodingException
     */
    public static JSONObject queryLogistics(String waybillNo){
        HZTServiceCodeEnum comReceHztsaSearchRoutes = HZTServiceCodeEnum.COM_RECE_HZTSA_SEARCH_ROUTES;//物流轨迹
        CallExpressServiceTools tools=CallExpressServiceTools.getInstance();
        Map<String, String> params = new HashMap<>();
        String timeStamp = String.valueOf(System.currentTimeMillis());
//        String msgData ="{\"trackingType\":1,\"trackingNumber\":[\""+id+"\"]\"checkPhoneNo\":\""+phone+"\"}";//无月结卡号通过运单和手机后4位即可
        JSONArray bodys = new JSONArray();
        JSONObject body = new JSONObject();
//        body.put("mailNo","SF3156830021031");
        body.put("mailNo",waybillNo);
        body.put("type","1");
        bodys.add(body);
        String msgData = bodys.toJSONString();
        params.put("partnerID", CLIENT_CODE);  // 顾客编码
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        params.put("serviceCode",comReceHztsaSearchRoutes.getCode());// 接口服务码
        params.put("timestamp", timeStamp);
        params.put("msgData", msgData);
//        params.put("msgDigest", tools.getMsgDigest(msgData,timeStamp, SFProperties.CHECK_WORD_BOX));
        try {
            params.put("msgDigest", tools.getMsgDigest(msgData,timeStamp, CHECK_WORD));

//        String result = HttpClientUtil.post(SFProperties.CALL_URL_BOX, params);
        String result = null;
            result = HttpClientUtil.post(CALL_URL, params);


        JSONObject apiResultData =JSONObject.parseObject(JSONObject.parseObject(result).getString("apiResultData"));
        String code = apiResultData.getString("code");
        if("200".equals(code)){
//        JSONObject resultMsgData = JSONObject.parseObject(apiResultData.getString("msgData"));
//        JSONArray routeResps = JSONArray.parseArray(resultMsgData.getString("routeResps"));
//        System.out.println(apiResultData);
        return apiResultData;
        }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return null;
     }
}
